classdef pbil < eda
    properties
       popsize;
       varsize;
       margprob;
       fitfunc; 
       iters;
       lambda;
       percentil;
    end
    methods
        function obj = pbil(popsize, varsize, fitfunc, lambda, percentil)
            obj.popsize = popsize;
            obj.varsize = varsize;
            obj.fitfunc = fitfunc;
            obj.margprob = repmat(0.5,1,varsize);
            obj.iters = 0;
            obj.lambda = lambda;
            obj.percentil = percentil;
        end
        function void = find(obj)
            while((obj.varsize - sum(obj.margprob)) > 0.01)
                obj.iters = obj.iters + 1;
                pop = pbil.sample(obj.margprob, obj.popsize, obj.varsize);
                fit = obj.fitfunc.getfit(pop);
                perctl = prctile(fit, obj.percentil);
                bestpop = pop(fit >= perctl, :);
                obj.margprob = obj.margprob.* (1-obj.lambda) + obj.lambda * (sum(bestpop)/ size(bestpop,1));
            end
        end
    end
    methods (Static)
        function pop = sample(margprob, popsize, varsize)
            pop = ones(popsize, 1)*margprob >= rand(popsize, varsize);
        end
    end
end